package string_question.leecode.test87;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/8 8:07
 */
public class Solution2 {
    // 动态规划
    // 扰乱字符串
    public boolean isScramble(String s1, String s2) {
        if (s1.length() != s2.length()) return false;
        if (s1.equals(s2)) return true;
        int n = s1.length();
        boolean[][][] dp = new boolean[n + 1][n][n];
        for (int len = 1; len <= n; len++) {
            for (int i = 0; i + len <= n; i++) {
                for (int j = 0; j + len <= n; j++) {
                    if (len == 1) {
                        dp[len][i][j] = s1.charAt(i) == s2.charAt(j);
                    } else {
                        for (int k = 1; k < len; k++) {
                            dp[len][i][j] = (dp[k][i][j] && dp[len - k][i + k][j + k])
                                    || (dp[k][i][j + len - k] && dp[len - k][i + k][j]);
                            if (dp[len][i][j]) break;
                        }
                    }
                }
            }
        }
        return dp[n][0][0];
    }
}
